import { Body, Controller, Get, Inject, Post } from '@nestjs/common';
import { ScriptService } from './script.service';
import { LotterySignAutoByTimeDto } from './dto/run-script.dto';
import { stringToTime } from 'src/utils/time';
import { SocketService } from '../socket/socket.service';
@Controller('script')
export class ScriptController {
  constructor(
    private readonly scriptService: ScriptService,
    @Inject('SocketService') private readonly socketService: SocketService,
  ) {
    this.socketService.on('script/claim', async () => {
      return await this.run();
    });
    this.socketService.on('script/runReset', async () => {
      return await this.runReset();
    });
    this.socketService.on('script/restartMitm', async () => {
      return await this.restartMitm();
    });
    this.socketService.on('script/sign', (options) => {
      return this.lotterySignAutoByTime(
        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        options.data as LotterySignAutoByTimeDto,
      );
    });
    this.socketService.on('script/sign-loop', () => {
      return this.lotterySignAutoLoop();
    });
    this.socketService.on('script/sign-fast', async () => {
      return await this.lotterySignAutoFast();
    });
    this.socketService.on('script/sign-fast-loop', () => {
      return this.lotterySignAutoFastLoop();
    });
    this.socketService.on('script/pdf-lotter-sign', (options) => {
      return this.pdfLotterSign(
        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        options.data as LotterySignAutoByTimeDto,
      );
    });
    this.socketService.on('script/sign-fast-by-time', (options) => {
      return this.lotterySignFastByTime(
        // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
        options.data as LotterySignAutoByTimeDto,
      );
    });
  }

  @Get('run')
  async run() {
    const { duration } = await this.scriptService.run({
      commandStr: this.scriptService.getScriptPath('claim.exe'),
      key: 'claim',
    });
    return { duration };
  }
  @Get('run-reset')
  async runReset() {
    const { duration } = await this.scriptService.runReset();
    return { duration };
  }
  @Get('restart-mitm')
  async restartMitm() {
    const { duration } = await this.scriptService.restartMitm();
    return { duration };
  }
  @Post('sign')
  lotterySignAutoByTime(@Body() options: LotterySignAutoByTimeDto) {
    const startTime = stringToTime(options.startTime);
    const endTime = stringToTime(options.endTime);
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    this.scriptService.lotterySignAutoByTime({
      startTime,
      endTime,
    });
    return 'ok';
  }
  @Post('sign-loop')
  lotterySignAutoLoop() {
    this.scriptService.lotterySignAutoLoop();
    return 'ok';
  }
  @Post('sign-fast')
  async lotterySignAutoFast() {
    await this.scriptService.lotterySignFast();
    return 'ok';
  }
  @Post('sign-fast-loop')
  lotterySignAutoFastLoop() {
    this.scriptService.lotterySignFastLoop();
    return 'ok';
  }
  @Post('pdf-lotter-sign')
  pdfLotterSign(@Body() options: LotterySignAutoByTimeDto) {
    const startTime = stringToTime(options.startTime);
    const endTime = stringToTime(options.endTime);
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    this.scriptService.pdfLotterSignAutoByTime({
      startTime,
      endTime,
    });
    return 'ok';
  }
  @Post('sign-fast-by-time')
  lotterySignFastByTime(@Body() options: LotterySignAutoByTimeDto) {
    const startTime = stringToTime(options.startTime);
    const endTime = stringToTime(options.endTime);
    void this.scriptService.lotterySignFastByTime({
      startTime,
      endTime,
    });
    return 'ok';
  }
}
